package document;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.BreakIterator;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

import pre_processing.StopWordRemoval;

public class Document {

	/**
	 * list of sentences in Document
	 */
	private List<String> words;

	/**
	 * ID of documents
	 */
	private int docID;

	/**
	 * constructor of Document
	 * 
	 * @param docIDIn
	 *            id document
	 * @param path
	 *            location of docs
	 */
	public Document(int docIDIn, String path) {

		/**
		 * Open the Document, assign DocID
		 */
		this.docID = docIDIn;
		words = new ArrayList<String>();
		File docFile = new File(path);

		/**
		 * Pilih per extensi : txt, doc, docx
		 */
		switch (path.substring(path.lastIndexOf(".") + 1, path.length())
				.toLowerCase()) {
		case "txt":
			readTXT(docFile);
			break;
		case "doc":
			readDOC(docFile);
			break;
		case "docx":
			readDOCX(docFile);
			break;
		default:
			System.out.println(path.substring(path.lastIndexOf("."),
					path.length()));
			break;
		}
		words=StopWordRemoval.stopWordRemoval((ArrayList<String>) words);
	}

	/**
	 * get array list of words
	 * 
	 * @return arrayList<String> of words
	 */
	public List<String> getWords() {
		return words;
	}

	/**
	 * return the document ID
	 * 
	 * @return document ID
	 */
	public int getDocID() {
		return docID;
	}

	/**
	 * read file TXT, convert it to words
	 * 
	 * @param file
	 *            that will be read
	 */
	private void readTXT(File file) {
		/**
		 * reader sebagai "pembaca"
		 */
		BufferedReader reader = null;
		try {
			/**
			 * pembaca membuka file yang akan dibaca
			 */
			reader = new BufferedReader(new FileReader(file));

			/**
			 * variabel
			 */
			String documentString = "";
			String line = null;

			/**
			 * ambil file per baris kemudian disusun ke documentString
			 */
			while ((line = reader.readLine()) != null) {
				documentString += line + " ";
			}

			/**
			 * split based on period
			 */
			BreakIterator iterator = BreakIterator
					.getSentenceInstance(Locale.US);
			iterator.setText(documentString);

			/**
			 * inisialisasi per kalimat
			 */
			int start = iterator.first();

			/**
			 * looping per kalimat
			 */
			for (int end = iterator.next(); end != BreakIterator.DONE; start = end, end = iterator
					.next()) {
				String sentence = documentString.substring(start, end);

				/**
				 * mengganti simbol menjadi spasi
				 */
				sentence = sentence.replaceAll("[.,!?'\"/;]", " ");

				/**
				 * potong berdasarkan spasi
				 */
				String[] allWords = sentence.split("\\s+");

				/**
				 * looping memasukkan word ke arraylist words
				 */
				for (String word : allWords) {
					/**
					 * sebelum dimasukkan -> case folding terlebih dahulu
					 */
					words.add(word.toLowerCase());
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (reader != null) {
					reader.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * read file doc, convert it to words, remove sentence contain only
	 * whitespace
	 * 
	 * @param file
	 */
	private void readDOC(File file) {
		try {
			/**
			 * cara membaca doc
			 */
			FileInputStream inputStream = new FileInputStream(file);
			HWPFDocument doc = new HWPFDocument(inputStream);
			WordExtractor extract = new WordExtractor(doc);
			
			/**
			 * buat variabel untuk memotong per kalimat
			 */
			BreakIterator iterator = BreakIterator
					.getSentenceInstance(Locale.US);
			
			/**
			 * looping per paragraph mendapatkan kalimat
			 */
			for (String paragraph : extract.getParagraphText()) {
				/**
				 * sentence is full of paragraph need to be break to sentences
				 */
				iterator.setText(paragraph);
				int start = iterator.first();
				
				/**
				 * looping per kalimat mendapatkan words
				 */
				for (int end = iterator.next(); end != BreakIterator.DONE; start = end, end = iterator
						.next()) {
					
					/**
					 * remove whitespace paragraph (setelah ditrim jika isi paragraph tidak kosong maka akan masuk if)
					 */
					if (paragraph.trim().length() > 0) {
						String sentence = paragraph.substring(start, end)
								.trim();
						
						/**
						 * remove and all simbol
						 */
						sentence = sentence.replaceAll("[^A-Za-z0-9]", " ");

						String[] allWords = sentence.split("\\s+");
						
						/**
						 * looping per kata untuk dimasukkan ke array words
						 */
						for (String word : allWords) {
							words.add(word);
						}
					}
				}
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void readDOCX(File file) {
		try {
			/**
			 * cara membaca docx
			 */
			FileInputStream inputStream = new FileInputStream(file);
			XWPFDocument doc = new XWPFDocument(inputStream);
			
			/**
			 * inisialisasi iterator paragraph
			 */
			Iterator<XWPFParagraph> paragraphIterator = doc
					.getParagraphsIterator();
			
			/**
			 * buat variabel untuk memotong per kalimat
			 */
			BreakIterator iterator = BreakIterator
					.getSentenceInstance(Locale.US);
			
			XWPFParagraph paragraph;
			StringBuffer buffer = new StringBuffer();
			
			/**
			 * looping per paragraph mendapatkan kalimat
			 */
			while (paragraphIterator.hasNext()) {
				paragraph = paragraphIterator.next();
				buffer.delete(0, buffer.length());
				
				/**
				 * paragraph dibagi menjadi run, lalu disusun di buffer
				 */
				for (XWPFRun run : paragraph.getRuns()) {
					buffer.append(run.toString());
				}
				/**
				 * sentence is full of paragraph need to be break to sentences
				 */
				String runString = buffer.toString();
				
				/**
				 * kalimat
				 */
				iterator.setText(runString);
				int start = iterator.first();
				
				/**
				 * looping per kalimat mendapatkan words
				 */
				for (int end = iterator.next(); end != BreakIterator.DONE; start = end, end = iterator
						.next()) {
					/**
					 * remove whitespace paragraph and simbol
					 */
					if (runString.trim().length() > 0) {
						String sentence = runString.substring(start, end)
								.trim();
						sentence = sentence.replaceAll("[^A-Za-z0-9]", " ");
						String[] allWords = sentence.split("\\s+");
						
						/**
						 * looping per kata untuk dimasukkan ke array words
						 */
						for (String word : allWords) {
							words.add(word);
						}
					}
				}
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
